package com.kexinde.wjifen.dao;

import java.util.List;

import com.kexinde.wjifen.util.JdbcTemplate;
import com.kexinde.wjifen.util.PageUtils;
import com.kexinde.wjifen.vo.PageInfo;
import com.kexinde.wjifen.vo.SearchShop;

public class SearchShopDao  {


	/**
	 * 微一下搜索商店，根据关键字去匹配商店的服务项，商店名字，按注册资金排序
	 * @param keyWord
	 * 查询关键字
	 * @param pageInfo
	 * PageInfo
	 * @return
	 * 根据模糊搜索返回商店集合
	 * @throws Exception
	 */
	public List<SearchShop> searchShopsByDefault(String keyWord,PageInfo pageInfo) throws Exception{
		String querySql = "select id,name,telNum,logo,tasteRank,environmentRank,serviceRank" +
			",productAmount,province,city,area,stress " +
			"from v_v_searchshop where name like ? OR service " +
			"like ? ORDER BY regMoney DESC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM v_v_searchshop WHERE"+
			" name like ? OR service like ?";
		String sqlKeyWord ="%"+keyWord+"%";
		Object [] params1 = {sqlKeyWord, sqlKeyWord};
		PageUtils pageUtils = new PageUtils(countSql,params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());
		
		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());
		
		Object [] params2 = {sqlKeyWord,sqlKeyWord,pageUtils.getFirst(),pageUtils.getMax()};
		
		return JdbcTemplate.queryList("com.kexinde.wjifen.vo.SearchShop", querySql, params2, 12);
	}
	
	
	/**
	 * 微一下搜索商店，根据关键字去匹配商店的服务项，商店名字，按访问次数排序
	 * @param keyWord
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<SearchShop> searchShopsByHot(String keyWord,PageInfo pageInfo) throws Exception{
		String querySql = "select id,name,telNum,logo,tasteRank,environmentRank,serviceRank" +
			",productAmount,province,city,area,stress " +
			"from v_v_searchshop where name like ? OR service " +
			"like ? ORDER BY visitTimes DESC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM v_v_searchshop WHERE"+
			" name like ? OR service like ?";
		String sqlKeyWord ="%"+keyWord+"%";
		Object [] params1 = {sqlKeyWord, sqlKeyWord};
		PageUtils pageUtils = new PageUtils(countSql,params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());
		
		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());
		
		Object [] params2 = {sqlKeyWord,sqlKeyWord,pageUtils.getFirst(),pageUtils.getMax()};
		
		return JdbcTemplate.queryList("com.kexinde.wjifen.vo.SearchShop", querySql, params2, 12);
	}
	
	
	/**
	 * 微一下搜索商店，根据关键字去匹配商店的服务项，商店名字，按访问次数排序
	 * @param keyWord
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<SearchShop> searchShopsByAmount(String keyWord,PageInfo pageInfo) throws Exception{
		String querySql = "select id,name,telNum,logo,tasteRank,environmentRank,serviceRank" +
		",productAmount,province,city,area,stress " +
		"from v_v_searchshop where name like ? OR service " +
		"like ? ORDER BY productAmount DESC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM v_v_searchshop WHERE"+
		" name like ? OR service like ?";
		String sqlKeyWord ="%"+keyWord+"%";
		Object [] params1 = {sqlKeyWord, sqlKeyWord};
		PageUtils pageUtils = new PageUtils(countSql,params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());
		
		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());
		
		Object [] params2 = {sqlKeyWord,sqlKeyWord,pageUtils.getFirst(),pageUtils.getMax()};
		
		return JdbcTemplate.queryList("com.kexinde.wjifen.vo.SearchShop", querySql, params2, 12);
	}

}
